package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.PromotionDao;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.base.PromotionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class PromotionServiceImpl implements PromotionService {
    @Autowired
    PromotionDao promotionDao;
    @Override
    public void save(Promotion promotion) {
        promotionDao.save(promotion);
    }

    @Override
    public Page<Promotion> findPageData(Pageable pageable) {
        return promotionDao.findAll(pageable);
    }

    @Override
    public PageBean<Promotion> findPageData(int page, int rows) {
        Pageable pageable = new PageRequest(page -1,rows);
        Page<Promotion> pageData = promotionDao.findAll(pageable);
       PageBean<Promotion> pageBean = new PageBean<>();
       pageBean.setPageData(pageData.getContent());
       pageBean.setTotlaCount(pageData.getTotalElements());
       System.out.println("page: "+page +"rows :   "+rows);
        return pageBean;
       // http://localhost:8080/bos_management/services/promotionService/pageQuery?page=1&rows=10

    }

    @Override
    public Promotion findPromotionById(Integer id) {
        return promotionDao.findOne(id);
    }

    @Override
    public void updateStatus(Date date) {
        promotionDao.updateStatus(date);
    }
}
